package com.student.controller;

import com.student.bean.User;
import com.student.service.UserService;
import com.student.utils.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/register")
    public AjaxResult register(@RequestParam(value = "username") String username,
                               @RequestParam(value = "password") String password,
                               @RequestParam(value = "sex") String sex,
                               @RequestParam(value = "age") Integer age,
                               @RequestParam(value = "email") String email,
                               @RequestParam(value = "nickname") String nickName) {
        User user = new User();
        user.setLoginname(username);
        user.setLoginpassword(password);
        user.setSex(sex);
        user.setAge(age);
        user.setEmail(email);
        user.setNickname(nickName);
        try {
            userService.register(user);
        } catch (Exception e) {
            return AjaxResult.getNo("账户名重复");
        }
        return AjaxResult.getOk("成功");
    }

    @ResponseBody
    @RequestMapping(value = "/userlogin")
    public AjaxResult login(@RequestParam(value = "username") String username,
                            @RequestParam(value = "password") String password) {
        AuthenticationToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return AjaxResult.getOk("登陆成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getNo("账号密码错误");
        }
    }

    @RequestMapping(value = "/logout")
    public void logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }

    @ResponseBody
    @RequestMapping(value = "/getname")
    public AjaxResult getName(){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        String name = user.getNickname();
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(1);
        ajaxResult.setMsg("成功");
        ajaxResult.setData(name);
        return ajaxResult;
    }

    //修改个人信息
    @ResponseBody
    @RequestMapping(value = "/selectUser")
    public List<User> selectUser(){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        String loginname = user.getLoginname();
        System.out.println("----  " +userService.selectUser(loginname));
        return userService.selectUser(loginname);
    }

    //修改
    @ResponseBody
    @RequestMapping(value = "/updatePerson1")
    public boolean updatePerson1(@RequestParam("nickname")String nickname,
                                @RequestParam("signature")String signature,
                                @RequestParam("sex")String sex,
                                @RequestParam("age")Integer age,
                                @RequestParam("phoneNumber")Integer phonenNumber,
                                @RequestParam("area")String area,
                                @RequestParam("constellationId")Integer constellationId,
                                @RequestParam("email")String email){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        String loginname = user.getLoginname();
        return userService.updateUser(nickname, signature, sex, age, phonenNumber, area, constellationId, email, loginname);

    }

}
